/**
 * Time interval module.
 */
define(['jquery'], function($) {
	var TimeInterval;
	TimeInterval = (function() {
		function TimeInterval() {}
		TimeInterval.prototype.init = function($timeIntervalWrap) {
			this.intervalOp($timeIntervalWrap);
		};
		TimeInterval.prototype.intervalOp = function($timeIntervalWrap) {
			var $timeSelectWrap = $timeIntervalWrap.find('.time-select-wrap'),
				$timeListOp = $timeIntervalWrap.find('.time-operate-list'),
				$timeBtns = $timeSelectWrap.find('.btn'),
      $allCkbs = $timeBtns.find('input'),
      $weekCkbs = $timeSelectWrap.find('.week-ckb'),
      $workCkbs = $weekCkbs.filter(':lt(5)');
      $weekendCkbs = $weekCkbs.filter(':gt(4)');
      $launchAllBtn = $timeListOp.find('.launch-all-btn'),
      $launchWorkBtn = $timeListOp.find('.launch-work-btn'),
      $launchWeekendBtn = $timeListOp.find('.launch-weekend-btn'),
      $timeSaveBtn = $('.time-save'),
      $timeCancelBtn = $('.time-cancel');

	    // All week.
	    $launchAllBtn.on('click', function (evt) {
	      evt.preventDefault();
	      $weekCkbs.prop('checked', true);
	      $timeBtns.removeClass('active');
	      $allCkbs.prop('checked', false);
	    });
	    // Work day.
	    $launchWorkBtn.on('click', function (evt) {
	      evt.preventDefault();
	      $launchAllBtn.trigger('click');
	      $weekendCkbs.trigger('click');
	    });
	    // Weekend.
	    $launchWeekendBtn.on('click', function (evt) {
	      evt.preventDefault();
	      $launchAllBtn.trigger('click');
	      $workCkbs.trigger('click');
	    });
	    $weekCkbs.on('click', function (evt) {
	      var $me = $(this),flag,
	        $ckbs = $me.parent().siblings('.btn-group').find('.btn'),
	        $ckb_input = $ckbs.find('input');

	      for (var i=0; i<$ckbs.length; i++) {
	        flag = $ckb_input[i].checked === this.checked ? true : false;
	        if (flag) {
	          this.checked ? $ckbs.eq(i).removeClass('active') : $ckbs.eq(i).addClass('active');
	          this.checked ? $ckb_input.eq(i).prop('checked',false) : $ckb_input.eq(i).prop('checked',true);
	        }
	      }
	    });

	    $timeBtns.on('click', function(evt){
	      var $me = $(this), $myCkb = $me.find('input'),
	        flag = !$myCkb.get(0).checked,
	        $myDay = $me.parent().siblings('.checkbox-inline').find('input');
	        $brother = $me.siblings().find('input');
	      for (var i=0; i<$brother.length; i++) {
	        flag = $brother[i].checked || flag;
	      }
	      $myDay.prop('checked',!flag);
	    });
		};

		return TimeInterval;
	})();

	return TimeInterval;
});